package cn.com.zsw.gblog.modules.gblog.controller;

import cn.com.zsw.gblog.config.cache.RedisCache;
import cn.com.zsw.gblog.modules.common.dto.LeaveMessageDTO;
import cn.com.zsw.gblog.modules.rocketmq.MqProducer;
import cn.com.zsw.gblog.modules.gblog.vo.WebSiteInfo;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.api.R;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @Author: shiwangZhou
 * @Description:
 * @Date: 2020-06-09 18:20
 */
@RestController
@RequestMapping("blog")
@Slf4j
@Api(tags = "博客", value = "BlogController")
public class BlogController {
    @Resource
    RedisCache redisCache;
    @Autowired
    MqProducer mqProducer;
    @Value("${site.notice}")
    private String notice;
    @Value("${site.desc}")
    private String desc;
    @Value("${site.avatar}")
    private String avatar;
    @Value("${site.slogan}")
    private String slogan;
    @Value("${site.name}")
    private String name;
    @Value("${site.domain}")
    private String domain;
    @Value("${file.download.domain}")
    private String fileDownloadUrl;
    @Value("${rocketmq.topic}")
    private String topic;

    @GetMapping("/site")
    public R<WebSiteInfo> site() {
        Long startTime = redisCache.getCacheObject("applicationStartTime");

        WebSiteInfo webSiteInfo = new WebSiteInfo();
        webSiteInfo.setNotice(notice);
        webSiteInfo.setDesc(desc);
        webSiteInfo.setAvatar(avatar);
        webSiteInfo.setSlogan(slogan);
        webSiteInfo.setName(name);
        webSiteInfo.setDomain(domain);
        webSiteInfo.setStartTime(startTime);
        return R.ok(webSiteInfo);
    }

    @GetMapping("/test")
    public String hello(String name) {
        return "hello " + name;
    }

    @PostMapping("/leave/message")
    public R leaveMessage(@RequestBody LeaveMessageDTO leaveMessageDTO) {
        log.info("个人博客有人留言，请注意查看:{}", leaveMessageDTO.getContent());
        mqProducer.sendMessage(JSON.toJSONString(leaveMessageDTO), topic);
        return R.ok(null);
    }


}
